package sg.edu.nus.iss.billing.dao.account;

import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.iss.billing.common.DbUtil;
import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.common.exception.BadDaoCallException;
import sg.edu.nus.iss.billing.common.exception.BadDbCallException;
import sg.edu.nus.iss.billing.common.exception.InvalidParamsException;
import sg.edu.nus.iss.billing.vo.account.AccountVO;

public class AccountDAO implements IAccountDAO {
	private ArrayList<String> data = null;
	private final static String FILE_PATH = "data//account.txt";

	public AccountDAO() {
		try {
			data = (ArrayList<String>) DbUtil.loadDb(FILE_PATH);
		} catch (BadDbCallException e) {
			data = new ArrayList<String>();
			System.out.println(e.getMessage());
		}
	}

	private boolean accountExist(String accountNo) {
		for (int i = 0; i < data.size(); i++) {
			try {
				AccountVO acct = new AccountVO();
				acct.setParameters(data.get(i));
				if (acct.getAccountNo().equals(accountNo)) {
					return true;
				}
			} catch (InvalidParamsException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	@Override
	public AccountVO getAccount(String accountNo) {
		try {
			for (int i = 0; i < data.size(); i++) {
				AccountVO acct = new AccountVO();
				acct.setParameters(data.get(i));
				if (acct.getAccountNo().equals(accountNo)) {
					return acct;
				}
			}
		} catch (InvalidParamsException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	@Override
	public void createAccount(AccountVO acct) throws BadDaoCallException,
			BadDbCallException {
		if (accountExist(acct.getAccountNo())) {
			throw new BadDaoCallException(
					"Account with same accountNo already exists!");
		} else {
			data.add(acct.serialised());
			DbUtil.saveDb(FILE_PATH, data);
		}
	}
	
	@Override
	public AccountVO getAcctByNric(String nric) {
		try {
			for (int i = 0; i < data.size(); i++) {
				AccountVO acct = new AccountVO();
				acct.setParameters(data.get(i));
				if (acct.getNric().equals(nric)) {
					return acct;
				}
			}
		}
		catch(InvalidParamsException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	@Override
	public List<AccountVO> getAllAccounts() {
		ArrayList<AccountVO> results = new ArrayList<AccountVO>();
		try {
			for (int i = 0; i < data.size(); i++) {
				AccountVO acct = new AccountVO();
				acct.setParameters(data.get(i));
				results.add(acct);
			} 	
		}
		catch (InvalidParamsException e) {
			System.out.println(e.getMessage());
		}
		return results;
	}

	@Override
	public void updateAcctStatus(String accountNo, String nric, String status) {
		AccountVO acct = new AccountVO();

		
		for (int i = 0; i < data.size(); i++) {
			try{
				acct.setParameters(data.get(i));
				if (acct.getAccountNo().equals(accountNo)) {
					acct.setStatus(status);
					data.set(i, acct.serialised());
					try {
						DbUtil.saveDb(FILE_PATH, data);
					} catch (BadDbCallException e1) {
						
					}
				}

			}catch (InvalidParamsException e) {
			
			}

			
		}
	}
}
